package xna.android.Telas;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import xna.android.Container;
import xna.android.Enemy;
import xna.android.Player;
import xna.android.Shot;
import xna.android.SoundManager;
import xna.android.Speed;
import xna.android.Sprite;
import xna.android.Sprite.ShotType;
import xna.android.SpriteFont;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.view.MotionEvent;
import android.view.SurfaceView;
import br.com.R;

public class Jogo extends SurfaceView 
{
	private Player player;
	private List<Enemy> enemies;
	private List<Shot> shots;
	
	private Container line;
	private Container redContainer;
	private Container blueContainer;
	
	private int score = 0;

	private SpriteFont spriteFont;

	private SoundManager mSoundManager;

	public SoundManager getmSoundManager() {
		return mSoundManager;
	}

	private boolean isPlayingBackGround = false;
	public boolean isPlaying = true;
	private Sprite exit;
	
	public Jogo(Context Content)
	{
		super(Content);

		this.mSoundManager = new SoundManager(Content);
		this.enemies = new ArrayList<Enemy>();
		this.shots = new ArrayList<Shot>();

		//http://developer.android.com/index.html
		//http://developer.android.com/guide/index.html
		//http://stackoverflow.com/questions/1077357/can-the-android-drawable-directory-contain-subdirectories
	}
	
	public void Play(){
		this.isPlaying = true;
		this.spriteFont = new SpriteFont(BitmapFactory.decodeResource(getResources(), R.drawable.fonte_verde));
		
		this.player = new Player(getResources(), R.drawable.nave, 50, 350, mSoundManager);
		
		this.blueContainer = new Container(getResources(), R.drawable.rect_blue, 300, 430, ShotType.BLUE);
		this.redContainer = new Container(getResources(), R.drawable.rect_red, 20, 430, ShotType.RED);
		this.line = new Container(getResources(), R.drawable.line, 20, 380, null);
		
		EnemyCreate();
		
		this.exit = new Sprite(BitmapFactory.decodeResource(getResources(), R.drawable.btn_exit), 20, 10, null, null);
	}
	
	private void EnemyCreate(){
		
		Random aleatorio = new Random();
		
		if (this.enemies.size() < 5)
			switch(aleatorio.nextInt(2)){
				case 0:
					this.enemies.add(new Enemy(getResources(), R.drawable.enemy_blue, aleatorio.nextInt(300), 0, new Speed(0, 1), ShotType.BLUE));
					break;
				case 1:
					this.enemies.add(new Enemy(getResources(), R.drawable.enemy_red, aleatorio.nextInt(300), 0, new Speed(0, 1), ShotType.RED));
					break;
			}
	}
	
	public void Clear(){
		this.spriteFont = null;
		this.player = null;
		this.redContainer = null;
		this.blueContainer = null;
		this.line = null;
		this.exit = null;
		this.score = 0;
		this.enemies.clear();
		this.shots.clear();
	}

	public void Update(int windowWidth, int windowHeight)
	{
		player.update(windowWidth, windowHeight);
		EnemyCreate();
		
		if (player.intersects(blueContainer))
			player.setType(ShotType.BLUE);
		else
			if (player.intersects(redContainer))
				player.setType(ShotType.RED);
		
		for (int k = 0; k < enemies.size(); k++) {
			enemies.get(k).update(windowWidth, windowHeight);
	
			if (player.intersects(enemies.get(k)) || enemies.get(k).intersects(line))
				isPlaying = false;
		}
		for (int i = 0; i < shots.size(); i++) {
			shots.get(i).update();
			if(!shots.get(i).isOnScreen())
				shots.remove(shots.get(i));
		}
		
		for (int i = 0; i < shots.size(); i++) {
			for (int k = 0; k < enemies.size(); k++) {
				if (player.getType() == enemies.get(k).getType() && enemies.get(k).intersects(shots.get(i))){
					shots.remove(shots.get(i));
					enemies.remove(enemies.get(k));
					score += 10;
					break;
				}
			}
		}
		
		
		if (!isPlayingBackGround){
			isPlayingBackGround = true;
			
		}
	}

	public void Draw(Canvas spriteBatch)
	{
		spriteBatch.drawColor(Color.BLACK);
		
		redContainer.draw(spriteBatch);
		blueContainer.draw(spriteBatch);
		line.draw(spriteBatch);
		
		for (int i = 0; i < enemies.size(); i++) {
			enemies.get(i).draw(spriteBatch);
		}
		for (int i = 0; i < shots.size(); i++) {
			if (player.getType() == ShotType.BLUE)
				shots.get(i).setBitmap(getResources(), R.drawable.shot_blue);
			else if (player.getType() == ShotType.RED)
				shots.get(i).setBitmap(getResources(), R.drawable.shot_red);
			shots.get(i).draw(spriteBatch);
		}
		
		player.draw(spriteBatch);
	
		spriteFont.drawString(spriteBatch, "Score: "+score, 240, 10);
		exit.draw(spriteBatch);
	}

	public boolean onTouchEvent(MotionEvent event) 
	{
		if (event.getAction() == MotionEvent.ACTION_DOWN)
		{
			// controle de evento com o sprite
			player.handleActionDown((int) event.getX(), (int) event.getY());

			if (player.isTouched()){
				player.getSoundManager().playSound("pistol");
				shots.add(new Shot(getResources(), R.drawable.shot_red, player.getX(), player.getY(), player.getType()));
			}
			
			exit.handleActionDown((int)event.getX(), (int)event.getY());
			if (exit.isTouched())
				isPlaying = false;
		}

		if (event.getAction() == MotionEvent.ACTION_MOVE)
		{
			if (player.isTouched())
			{
				player.setX((int) event.getX());
				player.setY((int) event.getY());
			}
		}

		if (event.getAction() == MotionEvent.ACTION_UP)
		{
			if (player.isTouched())
				player.setTouched(false);
			
			if (exit.isTouched())
				exit.setTouched(false);
		}
		
		return true;
	}
}
